package com.fancy.learncenter.ui.view;

import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.fancy.learncenter.R;
import com.fancy.learncenter.common.MyApplication;

import org.w3c.dom.Text;

/**
 * Created by Hyy on 2017/2/14.
 */

public class BindSchoolPopupwindow extends PopupWindow {
    View popopView;

    View locationView;

    Activity mContext;

    TextView learn_tv;
    TextView all_tv;
    TextView ngl_tv;

    public BindSchoolPopupwindow(Activity context, View locationView, BindSchoolCallBack bindSchoolCallBacks) {
        this.mContext = context;
        this.locationView = locationView;
        this.bindSchoolCallBack = bindSchoolCallBacks;
        popopView = LayoutInflater.from(MyApplication.context).inflate(R.layout.activity_bind_school_type, null);
        learn_tv = (TextView) popopView.findViewById(R.id.learn_tv);
        all_tv = (TextView) popopView.findViewById(R.id.all_tv);
        ngl_tv = (TextView) popopView.findViewById(R.id.ngl_tv);
        learn_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bindSchoolCallBack.learn();
                all_tv.setBackgroundColor(mContext.getResources().getColor(R.color.white));
                all_tv.setTextColor(mContext.getResources().getColor(R.color.bind_text_color));
                ngl_tv.setBackgroundColor(mContext.getResources().getColor(R.color.white));
                ngl_tv.setTextColor(mContext.getResources().getColor(R.color.bind_text_color));
                learn_tv.setBackgroundColor(mContext.getResources().getColor(R.color.yello));
                learn_tv.setTextColor(mContext.getResources().getColor(R.color.bind_text_color));
                dismiss();
            }
        });
        ngl_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bindSchoolCallBack.ngl();
                all_tv.setBackgroundColor(mContext.getResources().getColor(R.color.white));
                learn_tv.setBackgroundColor(mContext.getResources().getColor(R.color.white));
                ngl_tv.setBackgroundColor(mContext.getResources().getColor(R.color.yello));
                all_tv.setTextColor(mContext.getResources().getColor(R.color.bind_text_color));
                learn_tv.setTextColor(mContext.getResources().getColor(R.color.bind_text_color));
                ngl_tv.setTextColor(mContext.getResources().getColor(R.color.white));
                dismiss();
            }
        });
        all_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bindSchoolCallBack.all();
                ngl_tv.setBackgroundColor(mContext.getResources().getColor(R.color.white));
                learn_tv.setBackgroundColor(mContext.getResources().getColor(R.color.white));
                all_tv.setBackgroundColor(mContext.getResources().getColor(R.color.yello));
                ngl_tv.setTextColor(mContext.getResources().getColor(R.color.bind_text_color));
                learn_tv.setTextColor(mContext.getResources().getColor(R.color.bind_text_color));
                all_tv.setTextColor(mContext.getResources().getColor(R.color.white));
                dismiss();
            }
        });

        initPopupWionw();
    }


    private void initPopupWionw() {

        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        setOutsideTouchable(true);
        setAnimationStyle(R.style.share_animation);
        setContentView(popopView);
    }


    public void showPopupWindow() {
        if (!this.isShowing()) {
            WindowManager.LayoutParams wlp = mContext.getWindow().getAttributes();
            wlp.alpha = 0.5f;
            mContext.getWindow().setAttributes(wlp);
            mContext.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

            showAtLocation(locationView, Gravity.BOTTOM, 0, 0);
        } else {
            dismiss();
        }
    }

    @Override
    public void dismiss() {
        super.dismiss();

        WindowManager.LayoutParams wlp = mContext.getWindow().getAttributes();
        wlp.alpha = 1f;
        mContext.getWindow().setAttributes(wlp);
        mContext.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    }

    BindSchoolCallBack bindSchoolCallBack;

    public interface BindSchoolCallBack {
        abstract void learn();

        abstract void ngl();

        abstract void all();
    }
}
